package com.example.lastwork2.fragments.detail;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.example.lastwork2.R;
import com.example.lastwork2.adapter.OrderAdapter;
import com.example.lastwork2.adapter.OrderShopAdapter;
import com.example.lastwork2.model.Order;
import com.example.lastwork2.model.Shop;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class OrderDetailActivity extends AppCompatActivity {

    //控件
    private ListView orderShop;
    public static TextView sumPrice;

    //商品适配器
    private OrderShopAdapter orderShopAdapter;
    //存储所有返回的数据
    private List<Shop> shops;

    //用于接收一条商品信息
    private Shop shop;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);

        //获取控件
        getViews();

        //获取带来的订单ID
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        int orderId = Integer.parseInt(id);

        //根据orderID从数据库中找出对应商品
        getAllOrderShop(orderId);







    }

    private void getAllOrderShop(int orderId) {
        shops = new ArrayList<>();
        //向服务器发送请求
        sendToServer(orderId);


    }

    private void sendToServer(int orderId) {
        String url1 = "http://10.0.2.2:8080/bigwork/orderShop";

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(url1+"?orderId="+orderId);
                    URLConnection urlConnection = url.openConnection();
                    //接收服务器发来的数据
                    InputStream inputStream = urlConnection.getInputStream();
                    //创建字符流读取数据
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
                    String str = bufferedReader.readLine();
                    //服务器发送json数据成功
                    System.out.println(str);
                    //关闭流
                    bufferedReader.close();
                    inputStream.close();
                    //解析数据
                    Gson gson = new Gson();
                    //解析服务器发来的订单集合
                    shops = gson.fromJson(str,new TypeToken<List<Shop>>(){}.getType());

                    System.out.println("订单详情为:"+str);

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                //在子线程里面绑定适配器
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //实例化适配器对象
                        orderShopAdapter = new OrderShopAdapter(
                                OrderDetailActivity.this,
                                R.layout.activity_order_detail_item,
                                shops
                        );
                        //绑定适配器
                        orderShop.setAdapter(orderShopAdapter);
                    }
                });



            }
        }).start();






    }

    private void getViews() {
        orderShop = findViewById(R.id.order_shop);
        sumPrice = findViewById(R.id.sum_price);

    }
}